//
//  VisitorView.swift
//  微博Swift
//
//  Created by  justinchou on 16/2/23.
//  Copyright © 2016年  justinchou. All rights reserved.
//

import UIKit
import SnapKit

//设置VisitorViewDelegate 协议
protocol VisitorViewDelegate: NSObjectProtocol {
    
    func visitorViewDidLogin()
    
    func visitorViewDidRegister()
}

class VisitorView: UIView {
    
    /// 监听方法
    @objc private func clickRegisterButton() {
        delegate?.visitorViewDidRegister()
    }
    @objc private func clickLoginButton() {
        delegate?.visitorViewDidLogin()
    }
    
     /// 设置代理对象
    weak var delegate:VisitorViewDelegate?

    // MARK:- 懒加载控件
    /// 标题图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 遮罩图片
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    /// 小房子图片
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// 标签信息
    lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = ""
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(14)
        
        label.sizeToFit()
        
        return label
    }()
    /// 注册按钮
    private lazy var registerButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
    }()
    /// 登录按钮
    private lazy var loginButton: UIButton = {
        
        let btn = UIButton()
        
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        
        return btn
    }()
    
    // MARK: - 设置视图信息
    /// 设置访客视图信息
    ///
    /// - parameter imageName: 图像名称，如果是首页传入 nil
    /// - parameter message:   消息文字
    func setupInfo(imageName: String? = nil, message: String) {
        
        messageLabel.text = message
        
        guard let imageName = imageName else {
            startAnimation()
            
            return
        }
        
        homeIconView.hidden = true
        maskIconView.hidden = true
        iconView.image = UIImage(named: imageName)
    }
    
    // MARK:- 初始化函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.whiteColor()
        
        setupUI()
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

// MARK: - 设置界面
extension VisitorView {
    
    /// 界面布局
    func setupUI() {
        
        registerButton.addTarget(self, action: "clickRegisterButton", forControlEvents: .TouchUpInside)
        loginButton.addTarget(self, action: "clickLoginButton", forControlEvents: .TouchUpInside)
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
        
        maskIconView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton.snp_top).offset(-10)
            
        }
        
        homeIconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 224, height: 36))
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel)
            make.top.equalTo(registerButton.snp_top)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
    }
    /// 创建动画
    func startAnimation() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.removedOnCompletion = false
        
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
}
